<?php
$loader->requireOnce('includes/ManagerDashboard/Portlet.class.php');
class PortletRevenue extends Portlet {
	protected $departmentId;
	
	function PortletRevenue($name, $dateRange, $departmentId) {
		$this->name = $name;
		$this->dateRange = $dateRange;
		$this->_db = $GLOBALS['frame']['adodb']['db'];
		$this->departmentId = $departmentId;
	}
	
	protected function collectData() {
		$sql = "SELECT sum(real_payment) total, date(closed) closed
				FROM ticket
				WHERE date(closed) <= '$this->endDate' AND date(closed) >= '$this->startDate'
				AND department_id = $this->departmentId
				GROUP BY date(closed)";
		$result = $this->_db->GetAll($sql);
		$i = 0;
		foreach ($result as $row){
			$color = '#3030D0';
			if ($row['total'] <= 5000000) {
				$color = '#FF4444';
			}
			// Remember to use INTEGER data for the chart value (string will not be rendered)
			$this->values[$i] = new OFC_Charts_Line_Value(($row['total']/1000000), $color);
			$label = $this->getShortDate($row['closed']);
			$tip = $this->getWeekDay($row['closed']);
			$this->values[$i]->set_tooltip(L10nHelper::_("Revenue: %s",L10nHelper::renderNumber($row['total'])) . "<br>$tip");
			$this->xLabels[$i] = $label;	
			$i++;	
		}		
	}
	
	protected function buildChart() {
		// Create chart object and set backgound color
		$this->chart = new OFC_Chart();
		$this->chart->set_bg_colour("#FEFEFE");
		// Create chart title object and pass css param
		$title = new OFC_Elements_Title($this->name);
		$title->set_style("font-size: 13px; font-weight: bold; padding-bottom: 10px; color: #00248E");
		// Create chart object (each type is an object)
		$line_hollow = new OFC_Charts_Line_Hollow();
		// Set chart value
		$line_hollow->set_values($this->values);
		$line_hollow->set_halo_size(1);
		// Set lable, style for X axis
		$x_axis = new OFC_Elements_Axis_X();
		$x_axis->set_labels_from_array($this->xLabels);
		$x_axis->labels->set_rotate('diagonal');
		$x_axis->labels->set_size(9);
		$x_axis->set_colours("#555555", "#DDDDDD");
		// Set value range, step and style for Y axis
		$y_axis = new OFC_Elements_Axis_Y();
		$y_axis->set_range(0, 20, 2);
		$y_axis->set_colours("#555555", "#DDDDDD");
		$y_legend = new OFC_Elements_Legend_Y(L10nHelper::_("Revenue (Unit: million)"));
		$y_legend->set_style("font-size: 12px;");
		// Add all of them to chart object
		$this->chart->set_title($title);
		$this->chart->set_y_legend($y_legend);
		$this->chart->add_element($line_hollow);
		$this->chart->set_x_axis($x_axis);
		$this->chart->set_y_axis($y_axis);
		
	}
	
}
?>